//
//  APPInitManager.m
//  OlympicsTouristAttractionProject
//
//  Created by TFAppleWork-Summer on 2017/3/30.
//  Copyright © 2017年 TFAppleWork-Summer. All rights reserved.
//

#import "APPInitManager.h"
#import "AppDelegate.h"
//Uitil
#import <IQKeyboardManager/IQKeyboardManager.h>
#import <TFHttpManager.h>
#import <SWActionSheet.h>


@implementation APPInitManager

+ (void)initAppConfig {
    //设置IQKeyboardManager
    [APPInitManager p_setIQKeyboardManager];
    
    [APPInitManager p_setHTTPManager];
    
    id barButtonAppearanceInSearchBar = [UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], nil];
    [barButtonAppearanceInSearchBar setTitleTextAttributes:@{
                                                             NSForegroundColorAttributeName : TFSTYLE(buttonEnableBackgroundColor)
                                                             } forState:UIControlStateNormal];
    
    [[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[SWActionSheet class]]]setTintColor:TFSTYLE(buttonEnableBackgroundColor)];
//    [TFStyle sharedStyle];
}

+ (void)p_setIQKeyboardManager {
    IQKeyboardManager *shareManager = [IQKeyboardManager sharedManager];
    shareManager.shouldResignOnTouchOutside = YES;
    shareManager.toolbarDoneBarButtonItemText = @"完成";
//    shareManager.toolbarBarTintColor = [UIColor darkGrayColor];
//    shareManager.toolbarTintColor = [UIColor blueColor];
    [shareManager.disabledTouchResignedClasses addObjectsFromArray:@[]];
    [shareManager.disabledToolbarClasses addObjectsFromArray:@[]];
    [shareManager.disabledDistanceHandlingClasses addObjectsFromArray:@[]];
}

+ (void)p_setHTTPManager {
    //http://61.155.106.33:28083/ccenrun-admins-mo/
    //http://yunkangmall.njnwyy.com:8084/ccenrun-admins-mo/
    
    NSString *apiPath = @"http://yunkangmall.njnwyy.com:8084/ccenrun-admins-mo/";
//    NSString *apiPath = @"http://61.155.106.33:28083/ccenrun-admins-mo/";
    
    [TFHTTPMANAER registerBaseUrl:apiPath successValidator:^BOOL(id responseObject, NSError *error) {
        if (responseObject && !error) {
            BOOL status = [[responseObject valueForKey:@"returnCode"] boolValue] || [[responseObject valueForKey:@"msg"]isEqualToString:@"成功"];
            if ([[responseObject valueForKey:@"msg"] isEqualToString:@"用户登录超时"]) {
                [[NSNotificationCenter defaultCenter]postNotificationName:@"LOGIN_TIMEOUT" object:nil];
            }
            return status;
        }
        return NO;
    } returnedData:^id(id responseObject) {
        if ([responseObject valueForKey:@"data"]) {
            return [responseObject valueForKey:@"data"];
        }
        return responseObject;
    } returnedInfo:^id(id responseObject) {
        return [responseObject valueForKey:@"msg"];
    }];
    
    [TFHttpManager sharedHttpManager].showHud = ^(NSString *hudString) {
        AppDelegate* delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        if (delegate.window.rootViewController.presentedViewController) {
            [delegate.window.rootViewController.presentedViewController showHudInView:delegate.window.rootViewController.presentedViewController.view hint:hudString];
        }
        else {
            [delegate.window.rootViewController showHudInView:delegate.window.rootViewController.view hint:hudString];
        }
    };
    
    [TFHttpManager sharedHttpManager].dismissHud = ^{
        AppDelegate* delegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
        if (delegate.window.rootViewController.presentedViewController) {
            [delegate.window.rootViewController.presentedViewController hideHud];
        }
        else {
            [delegate.window.rootViewController hideHud];
        }
        
    };}

@end
